//
//  UIStandardPageControlView.m
//  vhECGTools
//
//  Created by jia yu on 2022/7/2.
//

#import "UIStandardPageControlView.h"
#import <vhECGTools/vhECGTools.h>
#import <ZMFoundation/ZMFoundation.h>
@interface UIStandardPageControlView(){
    NSBundle *myBundle;
    float scale;
}
@end

@implementation UIStandardPageControlView
@synthesize handler;
@synthesize prevPageButton;
@synthesize pageLabel;
@synthesize nextPageButton;
@synthesize currentPageNumber;
@synthesize allPageNumber;

-(id)initWithFrame:(CGRect)frame scale:(float)_scale{
    self = [super initWithFrame:frame];
    if (self){
        scale = _scale;
        UIImage *image = nil;
        NSBundle *frameBundle = [NSBundle ex_bundleWithName:@"vhECGTools"];
        myBundle = [[NSBundle alloc] initWithPath:[frameBundle pathForResource:@"UIStandardPageControlView" ofType:@"bundle"]];
        float space = -10.f * scale;
        self.backgroundColor = [UIColor clearColor];
        float buttonWidth = frame.size.height;
        prevPageButton = [[UIButton alloc] initWithFrame:CGRectMake(0.f, 0.f, buttonWidth, frame.size.height)];
        [prevPageButton addTarget:self action:@selector(prevButtonAction) forControlEvents:UIControlEventTouchUpInside];
        prevPageButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        image = [[UIImage alloc] initWithContentsOfFile:[myBundle pathForResource:@"pre_normal" ofType:@"png"]];
        image = [vhECGTools scaleImage:image to:CGSizeMake(image.size.width*2/scale, image.size.height*2/scale)];
        [prevPageButton setBackgroundImage:image forState:UIControlStateNormal];
        image = [[UIImage alloc] initWithContentsOfFile:[myBundle pathForResource:@"prev_disable" ofType:@"png"]];
        image = [vhECGTools scaleImage:image to:CGSizeMake(image.size.width*2/scale, image.size.height*2/scale)];
        [prevPageButton setBackgroundImage:image forState:UIControlStateDisabled];
        image = [[UIImage alloc] initWithContentsOfFile:[myBundle pathForResource:@"pre_click" ofType:@"png"]];
        image = [vhECGTools scaleImage:image to:CGSizeMake(image.size.width*2/scale, image.size.height*2/scale)];
        [prevPageButton setBackgroundImage:image forState:UIControlStateHighlighted];
        [prevPageButton setTitle:@"" forState:UIControlStateNormal];
        [self addSubview:prevPageButton];
        
        pageLabel = [[UILabel alloc] initWithFrame:CGRectMake(buttonWidth + space, 0.f, frame.size.width-2* (buttonWidth+space), frame.size.height)];
        pageLabel.textColor = [vhECGTools colorFromHEXString:@"294559"];
        pageLabel.backgroundColor = [UIColor clearColor];
        pageLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        pageLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:frame.size.height * 0.4f];
        pageLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:pageLabel];
        
        nextPageButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-buttonWidth, 0.f, buttonWidth, frame.size.height)];
        [nextPageButton addTarget:self action:@selector(nextButtonAction) forControlEvents:UIControlEventTouchUpInside];
        nextPageButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        image = [[UIImage alloc] initWithContentsOfFile:[myBundle pathForResource:@"next_normal" ofType:@"png"]];
        image = [vhECGTools scaleImage:image to:CGSizeMake(image.size.width*2/scale, image.size.height*2/scale)];
        [nextPageButton setBackgroundImage:image forState:UIControlStateNormal];
        image = [[UIImage alloc] initWithContentsOfFile:[myBundle pathForResource:@"next_disable" ofType:@"png"]];
        image = [vhECGTools scaleImage:image to:CGSizeMake(image.size.width*2/scale, image.size.height*2/scale)];
        [nextPageButton setBackgroundImage:image forState:UIControlStateDisabled];
        image = [[UIImage alloc] initWithContentsOfFile:[myBundle pathForResource:@"next_click" ofType:@"png"]];
        image = [vhECGTools scaleImage:image to:CGSizeMake(image.size.width*2/scale, image.size.height*2/scale)];
        [nextPageButton setBackgroundImage:image forState:UIControlStateHighlighted];
        [nextPageButton setTitle:@"" forState:UIControlStateNormal];
        [self addSubview:nextPageButton];
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame{
    NSLog(@"UIStandardPageControlView  -(id)initWithFrame:(CGRect)frame scale:(float)scale;");
    return nil;
}

-(void)refresh{
    if (1 == currentPageNumber){
        prevPageButton.enabled = NO;
    }else{
        prevPageButton.enabled = YES;
    }
    
    pageLabel.text = [NSString stringWithFormat:@"%d/%d", (int)currentPageNumber, (int)allPageNumber];
    
    if (allPageNumber == currentPageNumber){
        nextPageButton.enabled = NO;
    }else{
        nextPageButton.enabled = YES;
    }
}

#pragma mark - Set
-(void)setCurrentPageNumber:(NSInteger)_currentPageNumber{
    currentPageNumber = _currentPageNumber;
    [self refresh];
}

-(void)setAllPageNumber:(NSInteger)_allPageNumber{
    allPageNumber = _allPageNumber;
    [self refresh];
}

#pragma mark - Button Action
-(void)nextButtonAction{
    currentPageNumber ++;
    if (handler){
        handler(currentPageNumber);
    }
    [self refresh];
}

-(void)prevButtonAction{
    currentPageNumber --;
    if (handler){
        handler(currentPageNumber);
    }
    [self refresh];
}

@end
